<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('ckeditor'))
{
	function ckeditor($name)
	{
		$html .= "CKEDITOR.replace(".$name.")";
		return $html;
	}
}

/* ============================= */
if ( ! function_exists('input_row'))
{
	function input_row($name = '', $value = '', $label='', $extra = '')
	{
		$value = set_value($name, $value);
		$input_id = 'input_'.$name;
		$data = array(
			'name' => $name,
			'value' => $value,
			'id' => $input_id,
		);
		if(preg_match('/type="([^\'\"]+)"/', $extra, $matches))
		{
			$data['type'] = $matches[1];
			$extra = preg_replace('/type="([^\'\"]+)"/', '', $extra);
		}
		if(!preg_match('/class="(.+)"/', $extra, $matches))
		{
			$extra .= ' class="form-control"';
		}
		else
		{
			$extra = preg_replace('/class="(.+)"/', 'class="$1 form-control"', $extra);
		}
		$html = "\n";
		$html .= "<div class=\"form-group row_$name\">";
		$html .= "\n\t<label class=\"col-sm-2 control-label\" for=\"$input_id\">$label</label>";
		$html .= "\n\t<div class=\"controls col-sm-10\">";
		$html .= "\n\t\t".form_input($data, $value, $extra);
		$html .= "\n\t</div>";
		$html .= "\n</div>";
		return $html;
	}
}

if ( ! function_exists('inputdatetime_row'))
{
	function inputdatetime_row($name = '', $value = '', $label='', $extra = '')
	{
		$value = set_value($name, $value);
		$input_id = 'input_'.$name;
		$data = array(
			'name' => $name,
			'value' => $value,
			'id' => $input_id,
		);
		if(!preg_match('/class="(.+)"/', $extra, $matches))
		{
			$extra .= ' class="form-control"';
		}
		else
		{
			$extra = preg_replace('/class="(.+)"/', 'class="$1 form-control"', $extra);
		}
		$html = "\n";
		$html .= "<div class=\"form-group row_$name\">";
		$html .= "\n\t<label class=\"col-sm-2 control-label\" for=\"$input_id\">$label</label>";
		$html .= "\n\t<div class=\"controls col-sm-10\">";
		$html .= "\n\t\t<div class=\"input-group\">";
		$html .= "\n\t\t\t".form_input($data, $value, $extra);
		$html .= "\n\t\t\t<span class=\"add-on input-group-btn\">";
		$html .= "\n\t\t\t<button type=\"button\" class=\"btn btn-default hasTip\" title=\"".lang('select_datetime')."\"><i data-time-icon=\"icon-clock\" data-date-icon=\"icon-calendar\" class=\"icon-calendar\"></i></button>";
		$html .= "\n\t\t\t</span>";
		$html .= "\n\t\t</div>";
		$html .= "\n\t</div>";
		$html .= "\n</div>";
		return $html;
	}
}

if ( ! function_exists('inputmodal_row'))
{
	function inputmodal_row($name = '', $valuemodal = '', $label='', $link = '', $extra = '')
	{
		$value = $valuemodal['value'];
		$text = $valuemodal['text'];
		$value = set_value($name, $value);
		$input_id = 'input_'.$name;
		$data = array(
			'name' => $name,
			'value' => $value,
			'id' => $input_id,
		);
		if(!preg_match('/class="(.+)"/', $extra, $matches))
		{
			$extra .= ' class="form-control"';
		}
		else
		{
			$extra = preg_replace('/class="(.+)"/', 'class="$1 form-control"', $extra);
		}
		if(!preg_match('/disabled/', $extra, $matches))
		{
			$extra .= ' disabled="disabled"';
		}
		$html = "\n";
		$html .= "<div class=\"form-group row_$name\">";
		$html .= "\n\t<label class=\"col-sm-2 control-label\" for=\"$input_id\">$label</label>";
		$html .= "\n\t<div class=\"controls col-sm-10\">";
		$html .= "\n\t\t<div class=\"input-group\">";
		$html .= "\n\t\t\t".form_input('', $text, 'disabled="disabled" class="form-control"').form_hidden($name, $value);
		$html .= "\n\t\t\t<span class=\"add-on input-group-btn\">";
		if(is_array($link))
		{
			foreach($link as $key=>$val)
			{
				$html .= "\n\t\t\t<button type=\"button\" href=\"$key\" class=\"btn btn-default hasTip\" title=\"".lang('option').": $val\"><i class=\"icon-list\"></i> $val</button>";
			}
		}
		else
		{
			$html .= "\n\t\t\t<button type=\"button\" class=\"btn btn-default\"><i class=\"icon-list\"></i> ".lang('select')."</button>";
		}
		$html .= "\n\t\t\t</span>";
		$html .= "\n\t\t</div>";
		$html .= "\n\t</div>";
		$html .= "\n</div>";
		return $html;
	}
}

if ( ! function_exists('upload_row'))
{
	function upload_row($name = '', $label='', $extra = '')
	{
		$input_id = 'input_'.$name;
		$data = array(
			'name' => $name,
			'type' => 'file',
			'id' => $input_id,
		);
		$html = "\n";
		$html .= "<div class=\"form-group row_$name\">";
		$html .= "\n\t<label class=\"col-sm-2 control-label\" for=\"$input_id\">$label</label>";
		$html .= "\n\t<div class=\"controls col-sm-10\">";
		$html .= "\n\t\t".form_input($data, null, $extra);
		$html .= "\n\t</div>";
		$html .= "\n</div>";
		return $html;
	}
}

if ( ! function_exists('dropdown_row'))
{
	function dropdown_row($name = '', $options = array(), $selected = array(), $label='', $extra = '')
	{
		$selected = set_value($name, $selected);
		$input_id = 'input_'.$name;
		if(!preg_match('/class="(.+)"/', $extra, $matches))
		{
			$extra .= ' class="form-control"';
		}
		else
		{
			$extra = preg_replace('/class="(.+)"/', 'class="$1 form-control"', $extra);
		}

		if(!preg_match('/id="(.+)"/', $extra, $matches))
		{
			$extra .= ' id="'.$input_id.'"';
		}

		$html = "\n";
		$html .= "<div class=\"form-group row_$name\">";
		$html .= "\n\t<label class=\"col-sm-2 control-label\" for=\"$input_id\">$label</label>";
		$html .= "\n\t<div class=\"controls col-sm-10\">";
		$html .= "\n\t\t".form_dropdown($name, $options, $selected, $extra);
		$html .= "\n\t</div>";
		$html .= "\n</div>";
		return $html;
	}
}

if ( ! function_exists('textarea_row'))
{
	function textarea_row($name, $value = '', $label='', $extra = '')
	{
		$value = set_value($name, $value);
		$input_id = 'input_'.$name;
		$data = array(
			'name' => $name,
			'value' => $value,
			'id' => $input_id,
		);
		if(!preg_match('/class="(.+)"/', $extra, $matches))
		{
			$extra .= ' class="form-control"';
		}
		else
		{
			$extra = preg_replace('/class="(.+)"/', 'class="$1 form-control"', $extra);
		}
		$html = "\n";
		$html .= "<div class=\"form-group row_$name\">";
		$html .= "\n\t<label class=\"col-sm-2 control-label\" for=\"$input_id\">$label</label>";
		$html .= "\n\t<div class=\"controls col-sm-10\">";
		$html .= "\n\t\t".form_textarea($data, $value, $extra);
		$html .= "\n\t</div>";
		$html .= "\n</div>";
		return $html;
	}
}

if ( ! function_exists('buttons_row'))
{
	function buttons_row($buttons = array(), $type='')
	{
		$html = "\n";
		$html .= "<div class=\"form-group buttons\">";
		$html .= "\n\t<div class=\"controls col-sm-offset-2 col-sm-10\">";
		foreach($buttons as $btn)
		{
			$html .= "\n\t\t".$btn;
		}
		$html .= "\n\t</div>";
		$html .= "\n</div>";
		return $html;
	}
}

if ( ! function_exists('radios_row'))
{
	function radios_row($name, $options, $value='', $label='')
	{
		$value = set_value($name, $value);
		$html = "\n";
		$html .= "<div class=\"form-group row_$name\">";
		$html .= "\n\t<label class=\"col-sm-2 control-label\">$label</label>";
		$html .= "\n\t<div class=\"controls col-sm-10\">";
		$idx = 0;
		foreach($options as $val=>$txt)
		{
			$input_id = 'input_'.$name.$idx;
			$checked = ($val == $value);
			$data = array(
				'name' => $name,
				'value' => $val,
				'type' => 'radio',
				'id' => $input_id,
				'checked' => $checked,
			);
			$html .= "\n\t\t<label class=\"radio-inline\">";
			$html .= "\n\t\t\t".form_checkbox($data);
			$html .= "\n\t\t\t".$txt;
			$html .= "\n\t\t</label>";
			$idx++;
		}
		$html .= "\n\t</div>";
		$html .= "\n</div>";
		return $html;
	}
}

if ( ! function_exists('checkboxes_row'))
{
	function checkboxes_row($name, $options, $values='', $label='')
	{
		$values = set_value($name, $values);
		if(!is_array($values))
		{
			$values = array($values);
		}
		$html = "\n";
		$html .= "<div class=\"form-group row_$name\">";
		$html .= "\n\t<label class=\"col-sm-2 control-label\">$label</label>";
		$html .= "\n\t<div class=\"controls col-sm-10\">";
		$idx = 0;
		foreach($options as $val=>$txt)
		{
			$input_id = 'input_'.$name.$idx;
			$checked = in_array($val, $values);
			$data = array(
				'name' => $name.'[]',
				'value' => $val,
				'id' => $input_id,
				'checked' => $checked,
			);
			$html .= "\n\t\t<label class=\"checkbox-inline\">";
			$html .= "\n\t\t\t".form_checkbox($data);
			$html .= "\n\t\t\t".$txt;
			$html .= "\n\t\t</label>";
			$idx++;
		}
		$html .= "\n\t</div>";
		$html .= "\n</div>";
		return $html;
	}
}
/* ====================================== */

if ( ! function_exists('formOpen'))
{
	function formOpen($action = '', $method = 'post', $attributes = '', $hidden = array())
	{
		if (is_string($attributes))
		{
			$attributes = 'method="'.$method.'" role="form" '.$attributes;
			if(!preg_match('/class="(.+)"/', $attributes, $matches))
			{
				$attributes .= ' class="form-horizontal"';
			}
			else
			{
				$attributes = preg_replace('/class="(.+)"/', 'class="$1 form-horizontal"', $attributes);
			}
		}
		else
		{
			$attributes['method'] = $method;
			$attributes['role'] = 'form';
			if(isset($attributes['class']))
			{
				$attributes['class'] .= ' form-horizontal';
			}
			else
			{
				$attributes['class'] = 'form-horizontal';
			}
		}
		return form_open($action, $attributes, $hidden);
	}
}

if ( ! function_exists('formOpenMultipart'))
{
	function formOpenMultipart($action = '', $method = 'post', $attributes = array(), $hidden = array())
	{
		if (is_string($attributes))
		{
			$attributes = 'method="'.$method.'" role="form" '.$attributes;
			if(!preg_match('/class="(.+)"/', $attributes, $matches))
			{
				$attributes .= ' class="form-horizontal"';
			}
			else
			{
				$attributes = preg_replace('/class="(.+)"/', 'class="$1 form-horizontal"', $attributes);
			}
			$attributes .= ' enctype="multipart/form-data"';
		}
		else
		{
			$attributes['method'] = $method;
			$attributes['role'] = 'form';
			if(isset($attributes['class']))
			{
				$attributes['class'] .= ' form-horizontal';
			}
			else
			{
				$attributes['class'] = 'form-horizontal';
			}
			$attributes['enctype'] = 'multipart/form-data';
		}

		return form_open_multipart($action, $attributes, $hidden);
	}
}

if ( ! function_exists('formHidden'))
{
	function formHidden($name, $value = '', $recursing = FALSE)
	{
		$value = set_value($name, $value);
		return form_hidden($name, $value, $recursing);
	}
}

if ( ! function_exists('formInput'))
{
	function formInput($name, $value = '', $extra = '')
	{
		$value = set_value($name, $value);
		$data = array(
			'name' => $name,
			'value' => $value,
			'id' => 'input_'.$name,
		);
		if(!preg_match('/class="(.+)"/', $extra, $matches))
		{
			$extra .= ' class="form-control"';
		}
		else
		{
			$extra = preg_replace('/class="(.+)"/', 'class="$1 form-control"', $extra);
		}
		return form_input($data, $value, $extra);
	}
}

if ( ! function_exists('formPassword'))
{
	function formPassword($name, $value = '', $extra = '')
	{
		$value = set_value($name, $value);
		$data = array(
			'name' => $name,
			'value' => $value,
			'type' => 'password',
			'id' => 'input_'.$name,
		);
		if(!preg_match('/class="(.+)"/', $extra, $matches))
		{
			$extra .= ' class="form-control"';
		}
		else
		{
			$extra = preg_replace('/class="(.+)"/', 'class="$1 form-control"', $extra);
		}
		return form_input($data, $value, $extra);
	}
}

if ( ! function_exists('formUpload'))
{
	function formUpload($name, $value = '', $extra = '')
	{
		$value = set_value($name, $value);
		$data = array(
			'name' => $name,
			'value' => $value,
			'type' => 'file',
			'id' => 'input_'.$name,
		);
		return form_input($data, $value, $extra);
	}
}

if ( ! function_exists('formTextarea'))
{
	function formTextarea($name, $value = '', $extra = '')
	{
		$value = set_value($name, $value);
		$data = array(
			'name' => $name,
			'value' => $value,
			'id' => 'input_'.$name,
		);
		if(!preg_match('/class="(.+)"/', $extra, $matches))
		{
			$extra .= ' class="form-control"';
		}
		else
		{
			$extra = preg_replace('/class="(.+)"/', 'class="$1 form-control"', $extra);
		}
		return form_textarea($data, $value, $extra);
	}
}

if ( ! function_exists('formMultiselect'))
{
	function formMultiselect($name, $options = array(), $selected = array(), $extra = '')
	{
		if ( ! strpos($extra, 'multiple'))
		{
			$extra .= ' multiple="multiple"';
		}
		return formDropdown($name, $options, $selected, $extra);
	}
}

if ( ! function_exists('formDropdown'))
{
	function formDropdown($name, $options = array(), $selected = array(), $extra = '')
	{
		$selected = set_value($name, $selected);
		if(!preg_match('/class="(.+)"/', $extra, $matches))
		{
			$extra .= ' class="form-control"';
		}
		else
		{
			$extra = preg_replace('/class="(.+)"/', 'class="$1 form-control"', $extra);
		}
		return form_dropdown($name, $options, $selected, $extra);
	}
}

if ( ! function_exists('formCheckbox'))
{
	function formCheckbox($name, $value = '', $checked = FALSE, $extra = '')
	{
		$value = set_value($name, $value);
		$data = array(
			'name' => $name,
			'value' => $value,
			'id' => 'input_'.$name,
		);

		return form_checkbox($data, $value, $checked, $extra);
	}
}

if ( ! function_exists('formRadio'))
{
	function formRadio($name, $value = '', $checked = FALSE, $extra = '')
	{
		$data = array(
			'name' => $name,
			'value' => $value,
			'type' => 'radio',
			'id' => 'input_'.$name,
		);

		return form_checkbox($data, $value, $checked, $extra);
	}
}

if ( ! function_exists('formButton'))
{
	function formButton($data = '', $content = '', $extra = '')
	{
		if(!isset($data['class']) && !preg_match("/class=\"(.+)\"/i", $extra, $matches))
		{
			$extra .= 'class="btn btn-default"';
		}
		return form_button($data, $content, $extra);
	}
}
if ( ! function_exists('formClose'))
{
	function formClose($extra = '')
	{
		return form_close($extra);
	}
}


